#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;

class SafeFile
{
public:
    //在构造函数中托管资源
    SafeFile(FILE * fp)
    : _fp(fp)
    {
        cout << "SafeFile(FILE*)" << endl;
    }

    //提供访问资源的方法
    void write(const string & msg){
        fwrite(msg.c_str(),1,msg.size(),_fp);
    }

    //在析构函数中释放资源
    ~SafeFile(){
        if(_fp){
            fclose(_fp);
            cout << "fclose(_fp)" << endl;
        }
        cout << "~SafeFile()" << endl;
    }
private:
    FILE * _fp;
};

void test0(){
    string msg("hello,world!");
    SafeFile sf(fopen("wd.txt","a+"));
    sf.write(msg);

    /* FILE * p2 = fopen("www.txt","a+"); */
    /* SafeFile sf2(std::move(p2)); */
    /* SafeFile sf3(p2); */
}

void test1(){
    /* String(new int(1)); */
    new int(10);
}

int main(void){
    test0();
    return 0;
}
